package com.example.myblog.controller;

import com.example.myblog.common.AjaxResult;
import com.example.myblog.common.UserSessionUtils;
import com.example.myblog.entity.ArticleInfo;
import com.example.myblog.entity.UserInfo;
import com.example.myblog.service.ArticleService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/myartlist")
    public AjaxResult getMyArtList(HttpServletRequest request) {
        UserInfo userInfo = UserSessionUtils.getUser(request);
        if (userInfo == null) {
            return AjaxResult.fail(-1, "非法请求");
        }
        List<ArticleInfo> myArtList = articleService.getMyArtList(userInfo.getId());
        myArtList.forEach(art -> {
            if (art.getContent().length() > 60) {
                art.setContent((art.getContent().substring(0, 100) + "..."));
            }
        });//文章显示截断
        return AjaxResult.success(myArtList);
    }

    @RequestMapping("/listbypage")
    public AjaxResult getListByPage(Integer pindex, Integer psize) {
        if (pindex == null || pindex < 1) {
            pindex = 1;
        }
        if (psize == null || psize <= 1) {
            psize = 2;
        }
        int offset = (pindex - 1) * psize;//求分页的偏移量
        List<ArticleInfo> listByPage = articleService.getListByPage(psize, offset);
        listByPage.forEach(art -> {
            if (art.getContent().length() > 60) {
                art.setContent((art.getContent().substring(0, 100) + "..."));
            }
        });//文章显示截断
        int totalCnt = articleService.getCount();
        int pcount = (int) Math.ceil(totalCnt * 1.0 / psize);//每页显示数
        HashMap<String, Object> result = new HashMap<>();
        result.put("list", listByPage);
        result.put("pcount", pcount);
        return AjaxResult.success(result);
    }

    @RequestMapping("/add")
    public AjaxResult add(HttpServletRequest request, ArticleInfo articleInfo) {
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getContent()) ||
                !StringUtils.hasLength(articleInfo.getTitle())) {
            return AjaxResult.fail(-1, "参数错误");
        }
        UserInfo user = UserSessionUtils.getUser(request);
        if (user == null) {
            return AjaxResult.fail(-2, "用户未登录");
        }
        articleInfo.setUid(user.getId());
        return AjaxResult.success(articleService.add(articleInfo));
    }

    @RequestMapping("/del")
    public AjaxResult del(HttpServletRequest request, Integer id) {
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1, "参数错误");
        }
        UserInfo userInfo = UserSessionUtils.getUser(request);
        if (userInfo == null) {
            return AjaxResult.fail(-2, "用户未登录");
        }
        int ret = articleService.del(id, userInfo.getId());
        return AjaxResult.success(ret);
    }

    @RequestMapping("/update")
    public AjaxResult update(HttpServletRequest request, ArticleInfo articleInfo) {
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getContent()) ||
                !StringUtils.hasLength(articleInfo.getTitle())) {
            return AjaxResult.fail(-1, "参数错误");
        }
        UserInfo user = UserSessionUtils.getUser(request);
        if (user == null) {
            return AjaxResult.fail(-2, "用户未登录");
        }
        articleInfo.setUid(user.getId());
        articleInfo.setUpdatetime(LocalDateTime.now());
        return AjaxResult.success(articleService.update(articleInfo));
    }

    @RequestMapping("/detail")
    public AjaxResult getArtDetail(Integer id) {
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1, "参数错误");
        }
        return AjaxResult.success(articleService.getArtDetail(id));
    }

    @RequestMapping("/incr-rcount")
    public AjaxResult incrRCount(Integer id) {
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1, "参数错误");
        }
        return AjaxResult.success(articleService.incrReadCnt(id));
    }


}
